﻿package rd.hardcore 
{
	import aze.motion.eaze;
	import flash.desktop.NativeApplication;
	import flash.display.Screen;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.SharedObject;
	import flash.utils.setTimeout;
	import rd.hardcore.air.HardIcon;
	import rd.hardcore.events.EqDataEvent;
	import rd.hardcore.events.ResizeEvent;
	import rd.hardcore.events.SoundModifierEvent;
	import rd.hardcore.params.GlobalEvents;
	import rd.hardcore.params.GlobalParams;
	import rd.hardcore.ui.BackGround;
	import rd.hardcore.ui.config.ConfigPanel;
	import rd.hardcore.ui.controls.Controls;
	import rd.hardcore.ui.header.Header;
	import rd.hardcore.ui.SplashScreen;
	
	/*
	 * AIR Entry
	 * 
	 */ 
	
	public class Launcher extends Sprite
	{
		/* VAR */
		static private var INSTANCE	: Launcher;
		
		private var mainContent : Main;
		private var header		: Header;
		private var bg			: BackGround;
		private var splash		: SplashScreen;
		private var ctrls		: Controls;
		private var configPanel	: ConfigPanel;
	
		
		/* Launcher */
		public function Launcher()
		{	
			INSTANCE = this;
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align 	= StageAlign.TOP_LEFT;
			
			ctrls = new Controls();
			addChild(ctrls);
			
			bg = new BackGround();
			addChild(bg);
						
			mainContent = new Main();
			addChild(mainContent);
						
			configPanel = new ConfigPanel();
			addChild(configPanel);
			
			header = new Header();
			header.mask = bg.mainMask;
			addChild(header);		
				
			splash = new SplashScreen();
			addChild(splash);
			
			//Welcome harcore!
			addEventListener(Event.ACTIVATE, focusOnWindow);
			addEventListener(Event.DEACTIVATE, unfocusDaWindow);
			
			addEventListener(GlobalEvents.START_DRAG_APP, 	startDraging);
			addEventListener(ResizeEvent.PLEASE_MINIMIZE, 	minimizeWindow);
			addEventListener(Controls.OPEN_CLOSE, ctrls.handleOpenClose);
			addEventListener(SoundModifierEvent.CHANGE_SPEED, mainContent.changeSoundSpeed);
			addEventListener(Event.CLOSE, closeWindow);
			addEventListener(EqDataEvent.BOUCING_BASS_LEVEL, ctrls.updateSpeakers);
			
			var icon:HardIcon = new HardIcon();
			icon.addEventListener(ResizeEvent.PLEASE_MAXIMIZE, focusOnWindow);
			registerStartUp();
			
			stage.nativeWindow.x = (Screen.mainScreen.bounds.width 	- GlobalParams.WIDTH) >> 1;
			stage.nativeWindow.y = (Screen.mainScreen.bounds.height - GlobalParams.HEIGHT) >> 1;
			
		}
		
		
		/* Init // call by Singleton */
		public function init():void
		{			
			setTimeout(display, 3000);
			configPanel.init();
		}
		
		
		/* Display */
		private function display():void
		{
			removeChild(splash);
			
			eaze(bg).to(.3, { alpha:1 } );
			eaze(mainContent).to(.4, { alpha:1 } );
			eaze(header).to(.5, { alpha:1 } );
			eaze(configPanel).to(.5, { alpha:1 } );
		}
				
		/* Window Managers */
		private function minimizeWindow(e:ResizeEvent):void 
		{
			stage.nativeWindow.minimize();
		}
		
		private function closeWindow( evt : Event ) : void
		{
			if (NativeApplication.supportsSystemTrayIcon)
				stage.nativeWindow.visible = false;
			else
				stage.nativeWindow.close();
		}
		
		private function startDraging(e:Event):void 
		{
			stage.nativeWindow.startMove();
		}
		
		private function unfocusDaWindow(e:Event):void 
		{
			//au cas où (libérez mémoire)
		}
		
		private function focusOnWindow(e:Event):void 
		{	
			stage.nativeWindow.activate();
		}
		
		/**
		 * Initialise le lancement à l'ouverture de la session lors de la 1ère utilisation
		 */
		private function registerStartUp():void
		{
			var so:SharedObject = SharedObject.getLocal("hardcoreApp");
			if (so.data.firstRun)
			{
				so.data.firstRun = false;
				try {
					NativeApplication.nativeApplication.startAtLogin = false;
				}catch (err:Error)
				{
					//Bah ça plante quoi!
				}
			}
		}
		
		/*
		 * Singleton 
		 */
		
		static public function getInstance() : Launcher
		{		
			return INSTANCE;
		}
		 
		 
	}

}